<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>竞赛管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">竞赛名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="contestName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-inline">
                                <input type="text" name="status" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 批量删除
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="viewImage">
            <img src="{{d.url}}" lay-event="viewImg" onclick="previewImg(this)">
        </script>
        <script type="text/html" id="currentTableBar">
            <!--            <a class="layui-btn layui-btn-warm layui-btn-xs data-count-view" lay-event="view">预览</a>-->
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">题目管理</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        param = "?s&authorName=&originName=";
        table.render({
            elem: '#currentTableId',
            url: '/contest/list' + param,
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            title: '竞赛列表',
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 60, title: 'ID', sort: true},
                {field: 'name', width: 320, title: '竞赛名称'},
                {field: 'startTime', minWidth: 180, title: '开始时间', sort: true},
                {field: 'endTime', minWidth: 180, title: '结束时间', sort: true},
                {field: 'num', minWidth: 120, title: '参赛人数', sort: true},
                {field: 'status', minWidth: 100, title: '竞赛状态'},
                {title: '操作', minWidth: 250, toolbar: '#currentTableBar', align: "center"}
            ]],
            limit: 10,
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['prev', 'page', 'next', 'skip', 'count']
            },
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            param = "?s";
            if (data.field.contestName == null) {
                data.field.contestName = "";
            }
            if (data.field.status == null) {
                data.field.status = "";
            }
            param += "&name=" + data.field.contestName;
            param += "&status=" + data.field.status;
            table.render({
                elem: '#currentTableId',
                url: '/contest/list' + param,
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                title: '竞赛列表',
                cols: [[
                    {type: "checkbox", width: 50},
                    {field: 'id', width: 60, title: 'ID', sort: true},
                    {field: 'name', width: 320, title: '竞赛名称'},
                    {field: 'startTime', minWidth: 180, title: '开始时间', sort: true},
                    {field: 'endTime', minWidth: 180, title: '结束时间', sort: true},
                    {field: 'num', minWidth: 120, title: '参赛人数', sort: true},
                    {field: 'status', minWidth: 100, title: '竞赛状态'},
                    {title: '操作', minWidth: 250, toolbar: '#currentTableBar', align: "center"}
                ]],
                limit: 10,
                page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['prev', 'page', 'next', 'skip', 'count']
                },
                skin: 'line'
            });

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '创建竞赛',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../page/table/add_contest.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.confirm("确认删除所选内容吗?", function (index) {
                    data.forEach(function (item) {
                        //向服务器发送删除指令
                        $.ajax({
                            url: "/image/del/" + item.id,//请求地址
                            dataType: "json",//数据格式
                            type: "DELETE",//请求方式
                            async: false,//是否异步请求
                            success: function (data) {   //如何删除成功
                                if (data.flag) {//删除成功
                                    layer.msg(data.msg);
                                } else {
                                    layer.msg(data.msg);
                                }
                            },
                            error: function (data) {
                                layer.msg("服务器异常，请联系管理员!");
                            }
                        });
                    });
                    table.reload('currentTableId');
                    layer.close(index);
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '题目管理',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../page/table/contest_problem_manage.html',
                    success: function (layero, index) {
                        var iframe = window['layui-layer-iframe' + index];
                        //调用子页面的全局函数，向子页面传递参数
                        iframe.child(data);
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确认删除行吗？', function (index) {
                    //删除对应行（tr）的DOM结构，并更新缓存
                    obj.del();
                    layer.close(index);
                    //向服务器发送删除指令
                    $.ajax({
                        url: "/image/del/" + obj.data.id,//请求地址
                        dataType: "json",//数据格式
                        type: "DELETE",//请求方式
                        async: false,//是否异步请求
                        success: function (data) {   //如何删除成功
                            if (data.flag) {//删除成功
                                layer.msg(data.msg);
                            } else {
                                layer.msg(data.msg);
                            }
                        },
                        error: function (data) {
                            layer.msg("服务器异常，请联系管理员!");
                        }
                    });

                });
            } else if (obj.event === 'view') {

            }
        });

    });

    /*点击图片预览函数*/
    function previewImg(obj) {
        var img = new Image();
        img.src = obj.src;
        var height = img.height, width = img.width;
        if (img.height > 500) {
            height = '500px';
            width = (500 / (img.height)) * (img.width);
        }

        var imgHtml = "<img src='" + obj.src + "' height='" + height + "' width='" + width + "' />";
        //弹出层
        layer.open({
            type: 1,
            offset: 'auto',
            area: [width, 'auto'],
            shadeClose: true,//点击外围关闭弹窗
            scrollbar: false,//不现实滚动条
            title: "图片预览", //不显示标题
            content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
            cancel: function () {

            }
        });
    }

</script>

</body>
</html>
